//
//  PTSChangeButton.m
//  PTS80
//
//  Created by mac on 15/11/11.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "PTSChangeButton.h"

@interface PTSChangeButton ()

@property(nonatomic, assign) CGSize imageSize;

@property(nonatomic, assign) CGSize titleSize;

@property(nonatomic, assign) CGFloat contentWith;

@property(nonatomic, strong) UIFont *titleFont;

@property(nonatomic, assign) BOOL first;

@end

@implementation PTSChangeButton

- (void)awakeFromNib{
    
    [super awakeFromNib];
    self.changeImageAndTitle = Defult;
    
    self.clipsToBounds = YES;
}

- (void)setChangeImageAndTitle:(changeImgAndTitle)changeImageAndTitle{
    _changeImageAndTitle = changeImageAndTitle;
    if (self.changeImageAndTitle == ImgBTitleT){
        self.imageView.contentMode = UIViewContentModeBottom;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }else if (self.changeImageAndTitle == ImgTTitleB){
        self.imageView.contentMode = UIViewContentModeTop;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }else {
        if (self.imageView.image) {
            self.titleLabel.textAlignment = NSTextAlignmentLeft;
        }else{
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        
    }
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    
    if (self.changeImageAndTitle == ImgBTitleT){
        CGFloat imgX = 0;
        CGFloat imgY = 0;
        CGFloat imgW = contentRect.size.width;
        CGFloat imgH = contentRect.size.height * 0.7;
        return CGRectMake(imgX, imgY, imgW, imgH);
    }else if (self.changeImageAndTitle == ImgTTitleB){
        CGFloat imgX = 0;
        CGFloat imgY = contentRect.size.height * 0.3;
        CGFloat imgW = contentRect.size.width;
        CGFloat imgH = contentRect.size.height * 0.7;
        return CGRectMake(imgX, imgY, imgW, imgH);
    }else{
        
        CGFloat imgX = (self.frame.size.width - self.contentWith) / 2;
        CGFloat imgY = (contentRect.size.height - self.imageSize.height) / 2;
        
        if (imgX < 0) {
            imgX = 0;
        }
        
        if (self.frame.size.height >= self.imageSize.height) {
            return CGRectMake(imgX, imgY, self.imageSize.width, self.imageSize.height);
        }else{
            return CGRectMake(imgX, 0, self.imageSize.width, self.frame.size.height);
        }
        
        
    }
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    
    if (self.changeImageAndTitle == ImgBTitleT){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.titleLabel.font = self.titleFont;
        });
        CGFloat titleX = 0;
        CGFloat titleY = contentRect.size.height * 0.7;
        CGFloat titleW = contentRect.size.width;
        CGFloat titleH = contentRect.size.height * 0.3;
        return CGRectMake(titleX, titleY, titleW, titleH);
    }else if (self.changeImageAndTitle == ImgTTitleB){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.titleLabel.font = self.titleFont;
        });
        CGFloat titleX = 0;
        CGFloat titleY = 0;
        CGFloat titleW = contentRect.size.width;
        CGFloat titleH = contentRect.size.height * 0.3;
        return CGRectMake(titleX, titleY, titleW, titleH);
    }else{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.titleLabel.font = self.titleFont;
        });
        
        CGFloat imgX = (contentRect.size.width - self.contentWith) / 2;
        
        if (imgX < 0) {
            imgX = 0;
        }
        if (self.imageView.image) {
            CGFloat titleX = self.imageView.frame.size.width + imgX;
            CGFloat titleY = (contentRect.size.height - self.titleSize.height) / 2;
            return CGRectMake(titleX, titleY, self.titleSize.width, self.titleSize.height);
        }else{
            CGFloat titleX = contentRect.size.width / 2 - self.titleSize.width / 2;
            CGFloat titleY = (self.frame.size.height - self.titleSize.height) / 2;
            return CGRectMake(titleX, titleY, self.titleSize.width, self.titleSize.height);
        }
        
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (!self.first) {
        self.titleFont = self.titleLabel.font;
        CGFloat titleFontSize = self.titleLabel.font.pointSize;
        self.first = YES;
        
        NSMutableDictionary *attr = [NSMutableDictionary dictionary];
        attr[NSFontAttributeName] = [UIFont systemFontOfSize:titleFontSize];
        CGSize titleSize = [self.titleLabel.text sizeWithAttributes:attr];
        
        CGSize imageSize = self.currentImage.size;
        
        self.contentWith = titleSize.width + imageSize.width;
        
        self.imageSize = imageSize;
        
        self.titleSize = titleSize;
        
    }
    
}

@end
